package app.web.galgame.cgs.anaylse;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import cn.lily.com.enviroment.Enviroment;

/**
 * 采集文件分析
 * @author JiaJ
 *
 */
public class GalCollectsAnalyse {
	
	private String folderPath = "H:/待处理/FIN";
	
	public void analyse() {
		
		File folder = new File(folderPath);
		for(File childFolder:folder.listFiles()){
			
			String date = this.cutDate(childFolder.getName());
			
			System.out.println(childFolder.getName()+"开始处理......");
			
			if(childFolder.isDirectory()){
				
				if(!this.checkFolder(childFolder)){
					
					if(date==null || "".equals(date)){
						for(File file:childFolder.listFiles()){
							date = this.cutDate(file.getName());
							if(date != null && !"".equals(date)){
								break;
							}
						}
					}
					
					if(date==null || "".equals(date)){
						date = "000000";
					}
					
					File file = new File(childFolder.getPath().replaceAll("\\\\", "/")+"/"+date+".date");
					try {
						file.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
				
			}else{
				
				String fileName = childFolder.getName();
				String folderPath = childFolder.getPath().replaceAll("\\\\", "/");
				int lastP = folderPath.lastIndexOf(".");
				File newFolder = new File(folderPath.substring(0,lastP));
				newFolder.mkdirs();
				
				if(date==null || "".equals(date)){
					date = "000000";
				}
				
				childFolder.renameTo(new File(folderPath.substring(0,lastP)+"/"+childFolder.getName()));
				
				File file = new File(folderPath.substring(0,lastP)+"/"+date+".date");
				
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}

		}
		
	}
	
	public String cutDate(String name){
		
		StringBuffer sb = new StringBuffer();
		
		List<String> anaStr = new ArrayList<String>();
		
		boolean flag = false;
		for(int i=0;i<name.length();i++){
			
			char active = name.charAt(i);
			
			if(active == '[' || active =='('){
				flag = true;
				continue;
			}
			
			else if(active == ']' || active==')'){
				flag = false;
				anaStr.add(sb.toString());
				sb = new StringBuffer();
				continue;
			}
			
			if(flag){
				sb.append(active);
			}
			
		}
		
		for(String str:anaStr){
			
			if(str.length() != 6){
				continue;
			}
			
			Pattern p1=Pattern.compile("^[0-9]{0,}$");  
			if(p1.matcher(str).lookingAt()){
				return str;
			}
			
		}
		
		return "";
		
	}
	
	/**
	 * 是否已经存储日期
	 * @param folder
	 * @return
	 */
	public boolean checkFolder(File folder){
		for(File file:folder.listFiles()){
			if(file.getName().endsWith(".date")){
				return true;
			}
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();		
		Enviroment.getEnv().getLogBean().beginLog();
		
		GalCollectsAnalyse collect = new GalCollectsAnalyse();
		collect.analyse();	
		
		Enviroment.getEnv().getLogBean().endLog();
	}

}
